package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.ApartmentInfoMapper;
import com.atguigu.lease.web.admin.mapper.LeaseAgreementMapper;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Autowired
    public ApartmentFacilityService apartmentFacilityService;
    @Autowired
    public ApartmentLabelService apartmentLabelService;
    @Autowired
    public ApartmentFeeValueService apartmentFeeValueService;
    @Autowired
    public GraphInfoService graphInfoService;
    @Autowired
    public ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    public LeaseAgreementMapper leaseAgreementMapper;

    public boolean saveOrUpdateApartmentInfo(ApartmentSubmitVo apartmentSubmitVo,Long id1) {
        Long id=id1;
        boolean flag=(id!=0&&id!=null);
        saveOrUpdate(apartmentSubmitVo);

        //设备信息
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        for (Long facilityInfoId : facilityInfoIds) {
            ApartmentFacility apartmentFacility= ApartmentFacility.builder().build();
            apartmentFacility.setApartmentId(apartmentSubmitVo.getId());
            apartmentFacility.setFacilityId(facilityInfoId);
            if (flag){
                UpdateWrapper<ApartmentFacility> apartmentFacilityUpdateWrapper =new UpdateWrapper<>();
                apartmentFacilityUpdateWrapper.eq("apartment_id",apartmentSubmitVo.getId());
                apartmentFacilityService.update(apartmentFacility,apartmentFacilityUpdateWrapper);
            }else {
                apartmentFacilityService.save(apartmentFacility);
            }

        }
        //标签信息
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        for (Long labelId : labelIds) {
            ApartmentLabel apartmentLabel= ApartmentLabel.builder().build();
            apartmentLabel.setApartmentId(apartmentSubmitVo.getId());
            apartmentLabel.setLabelId(labelId);
            if (flag){
                UpdateWrapper<ApartmentLabel> labelUpdateWrapper = new UpdateWrapper<>();
                labelUpdateWrapper.eq("apartment_id",apartmentSubmitVo.getId());
                apartmentLabelService.update(apartmentLabel,labelUpdateWrapper);
            }else {
                apartmentLabelService.save(apartmentLabel);
            }

        }
        //杂费信息
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        for (Long feeValueId : feeValueIds) {
            ApartmentFeeValue apartmentFeeValue= ApartmentFeeValue.builder().build();
            apartmentFeeValue.setApartmentId(apartmentSubmitVo.getId());
            apartmentFeeValue.setFeeValueId(feeValueId);
            if (flag){
                UpdateWrapper<ApartmentFeeValue> feeValueUpdateWrapper=new UpdateWrapper<>();
                feeValueUpdateWrapper.eq("apartment_id",apartmentSubmitVo.getId());
                apartmentFeeValueService.update(apartmentFeeValue,feeValueUpdateWrapper);
            }else {
                apartmentFeeValueService.save(apartmentFeeValue);
            }
        }
        //图片信息
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        for (GraphVo graphVo : graphVoList) {
            GraphInfo graphInfo=new GraphInfo();
            graphInfo.setUrl(graphVo.getUrl());
            graphInfo.setName(graphVo.getName());
            graphInfo.setItemType(ItemType.APARTMENT);
            graphInfo.setItemId(apartmentSubmitVo.getId());
            if (flag){
                UpdateWrapper<GraphInfo> graphInfoUpdateWrapper =new UpdateWrapper<>();
                graphInfoUpdateWrapper.eq("item_id",apartmentSubmitVo.getId());
                graphInfoService.update(graphInfo,graphInfoUpdateWrapper);
            }else {
                graphInfoService.save(graphInfo);
            }
        }
        return true;
    }

    @Override
    public  IPage<ApartmentItemVo> findAllByCondition(IPage<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
        return apartmentInfoMapper.selectAllByCondition(page,queryVo);
    }

    @Override
    public ApartmentDetailVo getDetailById(Long id) {
        ApartmentDetailVo apartmentDetailVo=apartmentInfoMapper.selectDetailById(id);
        return apartmentDetailVo;
    }

    @Override
    public boolean removeApartmentById(Long id) {
        QueryWrapper<LeaseAgreement> leaseAgreementQueryWrapper=new QueryWrapper<>();
        leaseAgreementQueryWrapper.eq("apartment_id",id);
        List<LeaseAgreement> leaseAgreements = leaseAgreementMapper.selectList(leaseAgreementQueryWrapper);
        if (leaseAgreements==null){
            return false;
        }else {
            int i = apartmentInfoMapper.deleteById(id);
            QueryWrapper<ApartmentFacility> apartmentFacilityQueryWrapper=new QueryWrapper<>();
            apartmentFacilityQueryWrapper.eq("apartment_id",id);
            apartmentFacilityService.remove(apartmentFacilityQueryWrapper);

            QueryWrapper<ApartmentLabel> apartmentLabelQueryWrapper=new QueryWrapper<>();
            apartmentLabelQueryWrapper.eq("apartment_id",id);
            apartmentLabelService.remove(apartmentLabelQueryWrapper);

            QueryWrapper<ApartmentFeeValue> apartmentFeeValueQueryWrapper =new QueryWrapper<>();
            apartmentFeeValueQueryWrapper.eq("apartment_id",id);
            apartmentFeeValueService.remove(apartmentFeeValueQueryWrapper);
        }
        return false;
    }
}




